#include <GL/freeglut.h>
#include <cstdlib>
#include <cmath>

GLfloat theta = 0;

void reshape(int w, int h)
{
    // 防止窗口高度过小造成被零除错误
    if (0 == h)
        h = 1;
    GLfloat r = 1.0f * w / h;
    glViewport(0, 0, w, h); // 视口大小
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f, r, 1.0f, 100.0f);
    glMatrixMode(GL_MODELVIEW);
}

void display()
{
    glEnable(GL_DEPTH_TEST);
    glClearColor(0.8, 0.8, 0.8, 0);
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1, 0, 0);
    glLoadIdentity();
    glRotated(theta, 0, 0, 1);
    glutSolidCube(1);
    glutSwapBuffers();
}

void idle()
{
    theta += 0.1f;
    if (theta >= 360)
        theta = 0;
    glutPostRedisplay();
}

void main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(400, 400);
    glutCreateWindow("rotateCube");

    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutIdleFunc(idle);

    glutMainLoop();
}
